package com.trace.pplos.parse.service.impl;

import com.alibaba.fastjson.JSON;
import com.trace.pplos.parse.dao.dataobject.HjrkDetailInfoWithBLOBs;
import com.trace.pplos.parse.dao.dataobject.HjrkInfoWithBLOBs;
import com.trace.pplos.parse.dao.mapper.HjrkDetailInfoMapper;
import com.trace.pplos.parse.dao.mapper.HjrkInfoMapper;
import com.trace.pplos.parse.service.HjrkInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 2017/9/25.
 */
@Service
public class HjrkInfoServiceImpl implements HjrkInfoService {

    private static final Logger logger = LoggerFactory.getLogger(HjrkInfoServiceImpl.class);
    @Autowired
    private HjrkInfoMapper hjrkInfoMapper;
    @Autowired
    private HjrkDetailInfoMapper detailInfoMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(HjrkInfoWithBLOBs record) {
        try{
            if(record == null)return 0;
            //插入主表
            int hresult = hjrkInfoMapper.insertSelective(record);
            //插入子表
            List<HjrkDetailInfoWithBLOBs> detailInfoList = record.getDetailInfoList();
            if(null != detailInfoList && detailInfoList.size() > 0){
                for (HjrkDetailInfoWithBLOBs hd : detailInfoList) {
                    hd.setHjId(record.getId());
                    hresult = detailInfoMapper.insert(hd);
                }
            }
            return hresult;
        }catch(Exception e){
            logger.error(e.getMessage(), e, JSON.toJSON(record));
            return 0;
        }
    }
}
